package com.eqxiu.ieditor.api.controller;

import com.eqxiu.ieditor.dto.MallUserDTO;
import com.eqxiu.ieditor.interceptor.UserContext;
import eqxiu.passport.sdk.dto.UserDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @Classname 基础的controller 写公用方法调用
 * @Description TODO
 * @Author henry_chen
 * @Date 2019-05-08-08:59
 **/
public class BaseController {
    Logger logger = LoggerFactory.getLogger(BaseController.class);
    @Value("${userinfo.url}")
    private String userInfoURL;
   /**
    * 功能描述: <br>
    * 〈〉
    * @Param: [request]
    * @Return: java.lang.String
    * @Author: chenjiegen
    * @Date: 2019-05-08 09:18
    */
    public String getIpAddr(HttpServletRequest request) {
        String ipAddress = null;
        ipAddress = request.getHeader("x-forwarded-for");
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getRemoteAddr();
            if (ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")) {
                // 根据网卡取本机配置的IP
                InetAddress inet = null;
                try {
                    inet = InetAddress.getLocalHost();
                } catch (UnknownHostException e) {
                    logger.error("未知主机", e);
                }
                ipAddress = inet.getHostAddress();
            }

        }
        // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
        if (ipAddress != null && ipAddress.length() > 15) {
            if (ipAddress.indexOf(",") > 0) {
                ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
            }
        }
        return ipAddress;
    }

   /**
    * 功能描述:获取refererUrl
    * @Param: [request]
    * @Return: java.lang.String
    * @Author: chenjiegen
    * @Date: 2019-05-08 09:18
    */
    public String getRefererUrl(HttpServletRequest request) {
        return request.getHeader("referer");
    }

    /**
     * 功能描述: 获取request
     * 〈〉
     * @Param: [request]
     * @Return: java.lang.String
     * @Author: chenjiegen
     * @Date: 2019-05-08 09:19
     */
    public String readRequest(HttpServletRequest request) throws IOException {
        StringBuilder sb = new StringBuilder();
        try {
            String line;
            while ((line = request.getReader().readLine()) != null) {
                sb.append(line);
            }
        } finally {
            request.getReader().close();
        }
        return sb.toString();
    }

    /**
     * @Description 获取当前用户id
     * @Author SuiXiangyu
     * @Date 2019/7/11
     **/
    public String geUserId(HttpServletRequest request) {
        //获取登录用户id
        String userId = null;
        try {
            UserDTO u = UserContext.getUser();
            if (u != null && !"".equals(u.getId())) {
                userId = u.getId();
            }
        } catch (Exception e) {
            logger.error("get userId is wrong" + e);
            e.printStackTrace();
        }
        return userId;
    }
}
